<?php

function pr($arrs,$exit = 1){
    echo '<pre>';
    print_r($arrs);
    echo '</pre>';
}
    class Unlimited{
        protected $mysqli;
        public function __construct($config)
        {
            $this->mysqli = new mysqli($config['host'],$config['user'],$config['pwd']);
            $this->mysqli->select_db($config['db']);
            $this->mysqli->set_charset('utf8');
            if($this->mysqli->connect_errno){
                echo $this->mysqli->connect_error;
            }
        }
        public function getList($pid = 0,&$result=array(),$spac=0){
            $space = $spac+2;
            $sql = "select * from onepiece where pid = {$pid}";
            $rs = $this->mysqli->query($sql);

            while($row = $rs->fetch_assoc()){
//                pr($row);
                $row['name'] = str_repeat('&nbsp;&nbsp;',$space).$row['name'];
                $result[] = $row;
                $this->getList($row['id'],$result,$space);

            }
            return $result;
        }
//下拉
        public function  displayList(){
            $rs = $this->getList();
            $str = "<select name = 'cate'>";

            foreach ($rs as $key => $val) {
                $str .= "<option>{$val['name']}</option>";
            }
            $str .= "</select>";
            return $str;
        }

        public function getLink ($cid , &$result = array()){
            $sql = "select * from onepiece where id = {$cid}";
            $rs = $this->mysqli->query($sql);
            if($row=$rs->fetch_assoc()){
                $result[] = $row;
                $this->getLink($row['pid'],$result);
            }
            return array_reverse($result);
        }

//展现link
        public function displayLink($cid , &$result = array()){
            $rs = $this->getLink($cid);
            $str = '';
            foreach ($rs as $val){
                $str .="<a  href=''>{$val['name']}</a>>";
            }
            return $str;
        }
    }
    $config['host'] = 'localhost';
    $config['user'] = 'root';
    $config['pwd'] = '1';
    $config['db'] = 'tp_test';
    $class = new Unlimited($config);
//    pr($class);

    $arrs = $class->displayLink(10);
    pr($arrs);

?>